package org.thread.test04.demo10.homework.demo02;

import java.util.Random;
import java.util.concurrent.Callable;

/**
 * @author CSY
 * @date 2025 - 03 - 11
 */
public class MyCallable implements Callable<String> {
    private String name;
    /**
     * 总票数
     */
    private Integer ticketCount;

    public MyCallable(){}

    public MyCallable(String name, Integer ticketCount) {
        this.name = name;
        this.ticketCount = ticketCount;
    }

    @Override
    public synchronized String call() throws Exception {
        while (true) {
            Integer sellCount = new Random().nextInt(1,10);
            Thread.sleep(1000);
            if(ticketCount ==0){
                System.out.println(this.name+"没票了，停止售票");
                break;
            }
            if (sellCount <= ticketCount) {
                ticketCount-=sellCount;
                System.out.println(this.name+"成功出售"+sellCount+"张票，剩余："+ticketCount);
            }else{
                sellCount = ticketCount;
            }
        }
        return "没票了，停止售票";
    }
}
